package cn.tsa.tsp;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.tsp.TimeStampToken;
import org.bouncycastle.util.Store;

public class TSTokenInfo {

	private Date genTime;
	private ASN1ObjectIdentifier messageImprintAlgOID;
	private String policyID;
	private ArrayList<CertInfo> certList = new ArrayList<CertInfo>();

	public TSTokenInfo(TimeStampToken token) {
		genTime = token.getTimeStampInfo().getGenTime();
		messageImprintAlgOID = token.getTimeStampInfo().getMessageImprintAlgOID();
		policyID = token.getTimeStampInfo().getPolicy().getId();
		Store<?> certs = token.getCertificates();
		Iterator<?> it = certs.getMatches(null).iterator();
		while (it.hasNext()) {
			certList.add(new CertInfo((X509CertificateHolder) it.next())) ;
		}
	}

	@Override
	public String toString() {
		return "TimeStampTokenInfo [genTime:" + genTime + ", messageImprintAlgOID:" + messageImprintAlgOID
				+ ", policyID:" + policyID + ", certInfo:" + certList + "]";
	}

}

class CertInfo {
	private X500Name subject;
	private X500Name issuer;
	private Date startTime;
	private Date endTime;

	public CertInfo(X509CertificateHolder holder) {
		subject = holder.getSubject();
		issuer = holder.getIssuer();
		startTime = holder.getNotBefore();
		endTime = holder.getNotAfter();
	}

	@Override
	public String toString() {
		return "CertInfo [subject:" + subject + ", issuer:" + issuer  + ", startTime:" + startTime + ", endTime:" + endTime + "]";
	}

}